//借助整数的进制转化
#include <bitset>  
#include<iostream>
#include<sstream>
#include<stdlib.h>
using namespace std;
long hex_int(string s,int base)
{
	//string转成字符数组
	char* s1 = new char[s.size()];
	for (int i = 0; i < s.size(); i++)
	{
		s1[i] = s[i];
		
	}
	char* s2;
	//以base为底的数字以字符数组输入，得到10进制对应数字
	return strtol(s1, &s2, base);
}

int main()
{
   
    /*cout << "35的10进制" << std::dec << 35 << endl;
    cout << "35的16进制:" << std::hex << 35 << endl;
    cout << "35的2进制: " << bitset<8>(35) << endl;*/
	int i, n, j, k;
	string p;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> p;
		
		cout << std::oct << hex_int(p,16) ;
		if (i != n - 1)
		{
			cout << "\n";
		}
		
	}

    return 0;
}
